package com.huizu.project.apiController;

import com.huizu.common.annotation.Anonymous;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.core.page.TableDataInfo;
import com.huizu.common.enums.BusinessType;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.project.config.TerAspect;
import com.huizu.project.config.UserAspect;
import com.huizu.project.domain.ProLdgy;
import com.huizu.project.domain.ProLdzj;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.IProLdzjService;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 劳动总结报告Controller
 * 
 * @author zhangmx
 * @date 2024-06-05
 */
@Anonymous
@RestController
@RequestMapping("/api/proldzj")
public class ApiProLdzjController extends BaseController
{
    @Autowired
    private IProLdzjService proLdzjService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询劳动总结报告列表
     */
    @UserAspect
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProLdzj proLdzj,HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLdzj.setUserId(userId);
        startPage();
        List<ProLdzj> list = proLdzjService.selectProLdzjList(proLdzj);
        return getDataTable(list);
    }

    /**
     * 查询劳动总结报告列表
     */
    @UserAspect
    @PostMapping("/listPass")
    public TableDataInfo listPass(ProLdzj proLdzj,HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLdzj.setUserId(userId);
        proLdzj.setStatus("2");
        List<ProLdzj> list = proLdzjService.selectProLdzjList(proLdzj);
        return getDataTable(list);
    }

    /**
     * 查询劳动总结报告列表
     */
    @TerAspect
    @PostMapping("/terList")
    public TableDataInfo terList(@RequestBody ProLdzj proLdzj,HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        SysUserAuth sysUserAuthPram = new SysUserAuth();
        sysUserAuthPram.setUserId(Long.valueOf(userId));
        List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
        List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
        proLdzj.setBjIds(bjs);
        startPage();
        List<ProLdzj> list = proLdzjService.selectProLdzjList(proLdzj);
        return getDataTable(list);
    }

    /**
     * 查询劳动总结报告列表
     */
    @TerAspect
    @PostMapping("/terListMsg")
    public TableDataInfo terListMsg(@RequestBody ProLdzj proLdzj,HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        SysUserAuth sysUserAuthPram = new SysUserAuth();
        sysUserAuthPram.setUserId(Long.valueOf(userId));
        List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
        List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
        proLdzj.setBjIds(bjs);
        proLdzj.setStatus("1");
        startPage();
        List<ProLdzj> list = proLdzjService.selectProLdzjList(proLdzj);
        return getDataTable(list);
    }

    /**
     * 获取劳动总结报告详细信息
     */
    @PostMapping(value = "/getById")
    public AjaxResult getInfo(@RequestBody ProLdzj proLdzj)
    {
        String id = proLdzj.getId();
        return success(proLdzjService.selectProLdzjById(id));
    }

    /**
     * 新增劳动总结报告
     */
    @Log(title = "劳动总结报告", businessType = BusinessType.UPDATE)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProLdzj proLdzj,HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLdzj.setUserId(userId);
        return toAjax(proLdzjService.insertProLdzjApi(proLdzj));
    }

    /**
     * 通过
     */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ProLdzj proLd)
    {
        return toAjax(proLdzjService.pass(proLd));
    }

    /**
     * 驳回
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ProLdzj proLd)
    {
        return toAjax(proLdzjService.reject(proLd));
    }

    /**
     * 修改劳动总结报告
     */
    @PreAuthorize("@ss.hasPermi('project:proldzj:edit')")
    @Log(title = "劳动总结报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProLdzj proLdzj,HttpServletRequest request)
    {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);//获取用户id
        proLdzj.setUserId(userId);
        return toAjax(proLdzjService.updateProLdzj(proLdzj));
    }

    /**
     * 删除劳动总结报告
     */
    @PreAuthorize("@ss.hasPermi('project:proldzj:remove')")
    @Log(title = "劳动总结报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proLdzjService.deleteProLdzjByIds(ids));
    }
}
